//
//  AddExamArrangementViewController.m
//  CoachsBook
//
//  Created by huiyong li on 2022/8/22.
//

#import "AddExamArrangementViewController.h"
#import "OneTabButView.h"
#import "TwoTabButView.h"
#import "CoachsBookInputModel.h"
#import "CoachsBookTextFieldCell.h"
#import "CoachsBookTextViewCell.h"
#import "AddTraineesCell.h"
#import "TraineesCell.h"
#import "LongPickerView.h"
#import "LongPickerViewModel.h"
#import "PGDatePickManager.h"
#import "ExaminationRoomViewController.h"

@interface AddExamArrangementViewController ()<UITableViewDelegate,UITableViewDataSource,OneTabButViewDelegate,TwoTabButViewDelegate,UITextFieldDelegate,LongPickerViewDelegate,PGDatePickerDelegate>
@property(nonatomic, strong)NSMutableArray *traineesDataArray;
@property (nonatomic,strong) LongPickerView *myPickerView;

@end

@implementation AddExamArrangementViewController
-(NSMutableArray *)traineesDataArray
{
    if (!_traineesDataArray) {
        _traineesDataArray = [NSMutableArray array];
    }
    return _traineesDataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navgationheadv.titlelabel.text = self.VCType==1?@"录入考试安排":@"考试安排详情";
    
    OneTabButView *onetabbutview = [[OneTabButView alloc]initWithFrame:CGRectMake(0, APPheight-SafeAreaBottomHeight-65, APPwidth, 65)];
    onetabbutview.delegate = self;
    [onetabbutview.tabbut setTitle:@"确认" forState:UIControlStateNormal];
    [self.view addSubview:onetabbutview];
    onetabbutview.hidden = self.VCType==1?NO:YES;
    
    TwoTabButView *twotabbutview = [[TwoTabButView alloc]initWithFrame:CGRectMake(0, APPheight-SafeAreaBottomHeight-65, APPwidth, 65)];
    twotabbutview.delegate = self;
    [twotabbutview.lefttabbut setTitle:@"短信通知" forState:UIControlStateNormal];
    [twotabbutview.righttabbut setTitle:@"微信通知" forState:UIControlStateNormal];
    [self.view addSubview:twotabbutview];
    twotabbutview.hidden = self.VCType==1?YES:NO;
    [self getData];
}

- (void)getData
{
    NSArray *titlearr = @[];
    NSArray *promptarr = @[];
    NSArray *celltypearr = @[];
    if (self.VCType == 1) {
        titlearr = @[@"考试日期",@"考试科目",@"考试场地",@"参加考试学员",@"考试备注"];
        promptarr = @[@"请选择日期",@"请选择科目",@"请选择考试场地",@"",@""];
        celltypearr = @[@"1",@"1",@"1",@"6",@"4"];
        for (NSInteger i=0; i<titlearr.count; i++) {
            CoachsBookInputModel *model = [[CoachsBookInputModel alloc]init];
            model.title = titlearr[i];
            model.prompt = promptarr[i];
            model.cellType = [celltypearr[i] integerValue];
            model.showTopline = YES;
            model.showArrow = NO;
            if (i==0||i==1||i==2) {
                model.showArrow = YES;
            }
            [self.dataArray addObject:model];
            [self.traineesDataArray addObject:model];
        }
    }
    else{
        
    }

}

- (void)setupUI {
    [super setupUI];
    self.tableView.frame = CGRectMake(0, SafeAreaTopHight, APPwidth, APPheight-SafeAreaTopHight-SafeAreaBottomHeight-65);
    UIView *HeaderV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, 45)];
    HeaderV.backgroundColor = [UIColor whiteColor];
    
    UIView *myDataLineV = [[UIView alloc]initWithFrame:CGRectMake(15, 15, 3, 15)];
    myDataLineV.backgroundColor = rgba(22, 120, 255, 1);
    myDataLineV.layer.cornerRadius = 2;
    [HeaderV addSubview:myDataLineV];
    UILabel *myDataLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(myDataLineV.frame)+5, 15, 150, 15)];
    myDataLab.text = @"详细设置";
    myDataLab.textColor = [UIColor blackColor];
    myDataLab.textAlignment = 0;
    myDataLab.font = kPingFangSemibold(16);
    [HeaderV addSubview:myDataLab];
    
    self.tableView.tableHeaderView = HeaderV;
    [self setMyPickerView];
}

#pragma mark - 设置自定义选择器
-(void)setMyPickerView
{
    self.myPickerView = [[LongPickerView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, APPheight) dataArr:@[]];
    [[UIApplication sharedApplication].keyWindow addSubview:self.myPickerView];
    self.myPickerView.delegate = self;
    self.myPickerView.hidden = YES;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    CoachsBookInputModel *model = self.dataArray[section];
    if (model.cellType == 6) {
        return self.traineesDataArray.count+1;
    }
    return 1;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CoachsBookInputModel *model = self.dataArray[indexPath.section];
    if (model.cellType == 6) {
        if (indexPath.row == 0) {
            return 65;
        }
        else{
            return 50;
        }
    }
    else if (model.cellType == 4)
    {
        return 270;
    }
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CoachsBookInputModel *model = self.dataArray[indexPath.section];
    if (model.cellType == 1) {
        CoachsBookTextFieldCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookTextFieldCell"];
        if (!cell) {
            cell=[[CoachsBookTextFieldCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookTextFieldCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellFillWithModel:model];
        cell.contentField.tag = indexPath.section;
        cell.contentField.delegate = self;
        return cell;
    }
    else if (model.cellType == 4)
    {
        CoachsBookTextViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookTextViewCell"];
        if (!cell) {
            cell=[[CoachsBookTextViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookTextViewCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellFillWithModel:model];
        cell.CertificationTextViewBlock = ^(NSString * _Nonnull textViewText) {
            NSLog(@"99999=%@",textViewText);
            model.content = textViewText;
            [self.tableView reloadData];
        };
        return cell;
    }
    if (indexPath.row == 0) {
        AddTraineesCell *cell=[tableView dequeueReusableCellWithIdentifier:@"AddTraineesCell"];
        if (!cell) {
            cell=[[AddTraineesCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"AddTraineesCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.addBut addTarget:self action:@selector(AddTraineesCheng:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
        
    }
    TraineesCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TraineesCell"];
    if (!cell) {
        cell=[[TraineesCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TraineesCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.deleteBut addTarget:self action:@selector(deleteTraineesCheng:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    
}

#pragma mark  添加学员
-(void)AddTraineesCheng:(UIButton *)but
{
    
}
#pragma mark  删除学员
-(void)deleteTraineesCheng:(UIButton *)but
{
    
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:( UITextField*)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (model.showArrow) {
        UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        textField.inputView = dummyView; // Hide keyboard,but show blinking cursor
        NSMutableArray *PickerViewModelArr = [[NSMutableArray alloc]init];
        [PickerViewModelArr removeAllObjects];
        self.myPickerView.tag = textField.tag;
        if (textField.tag == 0) {
            PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
            datePickManager.isShadeBackground = true;
            datePickManager.cancelButtonTextColor = rgba(153, 153, 153, 1);
            datePickManager.confirmButtonTextColor = rgba(22, 120, 255, 1);
            datePickManager.headerViewBackgroundColor = [UIColor whiteColor];
            PGDatePicker *datePicker = datePickManager.datePicker;
            datePicker.tag = textField.tag;
            datePicker.delegate = self;
            datePicker.datePickerType = PGDatePickerTypeVertical;
            datePicker.isHiddenMiddleText = false;
            datePicker.datePickerMode = PGDatePickerModeDate;
            [self presentViewController:datePickManager animated:false completion:nil];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
            dateFormatter.dateFormat = @"yyyy-MM-dd";
            NSDate *date = [dateFormatter dateFromString: model.content];
            [datePicker setDate:date animated:true];
            datePicker.minimumDate = [NSDate date];
        }
        else if (textField.tag == 1) {
            NSArray *titlearr = @[@"科一",@"科二",@"科三",@"科四"];
            for (NSString *title in titlearr) {
                LongPickerViewModel *model =  [[LongPickerViewModel alloc]init];
                model.title = title;
                [PickerViewModelArr addObject:model];
            }
            self.myPickerView.pickerViewType = LongSingleLine;
            self.myPickerView.dataArr = PickerViewModelArr;
            self.myPickerView.hidden = NO;
        }
        else if (textField.tag == 2)
        {
            [self.view endEditing:YES];
            ExaminationRoomViewController *vc = [[ExaminationRoomViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
   
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (textField.text.length) {
        model.content = textField.text;
    }
}

#pragma mark - CYWKLPickerViewDelegate
-(void)choosPickerView:(LongPickerView *)pickerView WithRowStr:(NSString *)rowStr AndRowNum:(NSInteger)row
{
    CoachsBookInputModel *model = self.dataArray[pickerView.tag];
    model.content = rowStr;
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:0 inSection:pickerView.tag];
    CoachsBookTextFieldCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.contentField.text = rowStr;
    [self.view endEditing:YES];
    
}

-(void)closePickerView:(LongPickerView *)pickerView
{
    [self.view endEditing:YES];
}

#pragma mark - PGDatePickerDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    NSLog(@"dateComponents = %@", dateComponents);
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-d";
    NSDate *time = [formatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%ld",[dateComponents year],[dateComponents month],[dateComponents day]]];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *daystr = [formatter stringFromDate:time];
    NSLog(@"%@",daystr);
    CoachsBookInputModel *model = self.dataArray[datePicker.tag];
    model.content = daystr;
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:0 inSection:datePicker.tag];
    CoachsBookTextFieldCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.contentField.text = daystr;
    [self.view endEditing:YES];
    
    
}

-(void)cancelDatePicker:(PGDatePicker *)datePicker
{
    [self.view endEditing:YES];
}


@end
